package com.qububu.common.notice.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.notice.dto.NoticeInfoDto;
import com.qububu.common.notice.dto.NoticeInfoDto.NoticeType;
import com.qububu.common.notice.service.NoticeService;
import com.qububu.test.one.dto.OneMapMarkerDtoLngLat;
import com.qububu.utils.DateUtil;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

/**
 * @概述 系统公告的控制
 * */
@Controller
@RequestMapping("/notice")
public class NoticeControl {

	@Resource
	private NoticeService noticeService;
	
	/**
	 * @author HQ
	 * @功能 查找全部系统公告
	 */
	@RequestMapping("/queryNoticeList")
	public String queryNoticeList(HttpServletRequest request, HttpServletResponse response){
		
		//List<NoticeInfoDto> noticeInfoDto = noticeService.getNoticeList();
		//request.setAttribute("noticeList", noticeInfoDto);
		
		return "testArticleList";
	}
	
	/**
	 * @author HQ
	 * @功能 查找全部社区公告
	 */
	
	/**
	 * @author HQ
	 * @功能 查找公告id查看公告详情
	 */
	@RequestMapping("/showNoticeInfo")
	public String queryNoticeInfo(HttpServletRequest request, HttpServletResponse response){
		
		String noticeId = ParamConverter.assertNotEmpty(request.getParameter("noticeId"));
		String decNoticeId = SystemUtil.decrypt(noticeId);
		int realNoticeId = ParamConverter.convertToInteger(decNoticeId);
		
		NoticeInfoDto noticeInfo = noticeService.getNoticeInfo(realNoticeId);
		request.setAttribute("noticeInfo", noticeInfo);

		return "showOneNotice";
	}
	
	/**
	 * @author liuhongjun
	 * 地图marker信息
	 * */
	@RequestMapping(value="/mapMarkerData")
	@ResponseBody
	public Map<String,Object> mapMarkerData(HttpServletRequest request,HttpServletResponse response){
	
		String num = request.getParameter("clickNumber");
		int  marKerTypeNum = Integer.parseInt(num) ;
		Map<String, Object> markerMapLngLat = new HashMap<String, Object>();
		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		
		OneMapMarkerDtoLngLat mapMarkerDtoLngLat = new OneMapMarkerDtoLngLat();
		Map<String , Object> josonMap = new HashMap<String, Object>();
		Map<String , Object> josonMap1 = new HashMap<String, Object>();
		
			if(marKerTypeNum == 0){
				double x = 103.98757;
				double y = 30.580274;
				mapMarkerDtoLngLat.setX(x);
				mapMarkerDtoLngLat.setY(y);
				
				
				josonMap.put("x",x);
				josonMap.put("y",y);
				josonMap.put("title","住宿(金坤静园小区东)");
				josonMap.put("address","天津南路503号 附近");
				josonMap.put("phoneNumber","18200388503");
				list.add(josonMap);
				
				josonMap1.put("x","103.98757");
				josonMap1.put("y","34.580274");
				josonMap1.put("title","山水宾馆");
				josonMap1.put("address","皮库胡同45");
				josonMap1.put("phoneNumber","010-66063388");
				list.add(josonMap1);
			}
			if(marKerTypeNum == 1){
				double x = 100.98757;
				double y = 30.580274;
				mapMarkerDtoLngLat.setX(x);
				mapMarkerDtoLngLat.setY(y);
				
				
				josonMap.put("x",x);
				josonMap.put("y",y);
				josonMap.put("title","金坤静园XXXX");
				josonMap.put("address","天津南路503号 附近");
				josonMap.put("phoneNumber","18200388503");
				list.add(josonMap);
				
				josonMap1.put("x","100.98757");
				josonMap1.put("y","34.580274");
				josonMap1.put("title","山水宾馆XXXX");
				josonMap1.put("address","皮库胡同45");
				josonMap1.put("phoneNumber","010-66063388");
				list.add(josonMap1);
			}
				markerMapLngLat.put("list",list);
			
		return markerMapLngLat;
		
		
	}
	/**
	 * @author ZCW
	 * @功能 进入主页
	 * @备注 
	 * */
	@RequestMapping("/info")
	public String intoEmporium() {
		
		return "index";
	}
	/*
	 * 公告展示
	 * */
	@RequestMapping("/showSiteAndComm")
	public String showSiteAndComm(HttpServletRequest req) {
		
		List<NoticeInfoDto> allNotice = noticeService.getAllNotice();
		Map<String, Object> noticeMap = getNoticeMessagesMap(allNotice);
		
		req.setAttribute("siteList", noticeMap.get("siteList")); // 系统公告列表
		req.setAttribute("communityList", noticeMap.get("communityList"));// 社区公告列表
		return "showSiteAndCommunity";
	}
	
	/**
	 * 找到notice信息列表
	 * */
	private Map<String, Object> getNoticeMessagesMap(List<NoticeInfoDto> allNotice) {
		Map<String, Object> noticeListMap = new HashMap<String, Object>();
		List<NoticeInfoDto> siteNoticeInfoList = new ArrayList<NoticeInfoDto>();
		List<NoticeInfoDto> communityNoticeInfoList = new ArrayList<NoticeInfoDto>();
		
		for (NoticeInfoDto notice : allNotice) {
			if (notice.getNoticeType().equals(NoticeInfoDto.NoticeType.SITE)) {
				siteNoticeInfoList.add(notice);
			}
			if (notice.getNoticeType().equals(NoticeInfoDto.NoticeType.COMMUNITY)) {
				communityNoticeInfoList.add(notice);
			}
		}
		
		noticeListMap.put("siteList", siteNoticeInfoList);
		noticeListMap.put("communityList", communityNoticeInfoList);
		
		return noticeListMap;
	}
	
	@RequestMapping("/play")
	public String play() {
		
		return "play";
	}
	
	/**************************管理员专用*****************************/
	/*
	 * 进入公告添加
	 * */
	@RequestMapping("admin/addSiteAndComm")
	public String addSiteAndComm() {
		
		return "addSiteAndCommunity";
	}
	
	@RequestMapping("admin/infoAdminNotice")
	public String adminNotice(HttpServletRequest req) {
		
		List<NoticeInfoDto> allNotice = noticeService.getAllNotice();
		Map<String, Object> noticeMap = getNoticeMessagesMap(allNotice);
		
		req.setAttribute("siteList", noticeMap.get("siteList"));
		req.setAttribute("communityList", noticeMap.get("communityList"));
		
		return "adminNotice";
	}
	
	/**
	 * @author HQ
	 * @功能 添加公告，网络公告和社区公告传入的id不同
	 * @param noticeId=0时网络公告，noticeId=1社区公告
	 */
	@RequestMapping("admin/addNoticeInfo")
	public String addNoticeInfo(HttpServletRequest request, HttpServletResponse response){
	
		String noticeId = SystemUtil.getUUIDString();
		String noticeTitle = SystemUtil.convertSpecialMark(ParamConverter.assertNotEmpty(request.getParameter("noticeTitle")));
		String noticeContext = SystemUtil.removeAllScriptFromHtml(ParamConverter.assertNotEmpty(request.getParameter("noticeContent")));
		String noticeType = ParamConverter.assertNotEmpty(request.getParameter("noticeType"));
		String summaryText = SystemUtil.removeAllScriptFromHtml(ParamConverter.assertNotEmpty(request.getParameter("noticeSummary")));
		String noticePicUrl = "";
		if (!ParamConverter.isNullOrEmpty(request.getParameter("noticePicUrl") ) ) {
			noticePicUrl = request.getParameter("noticePicUrl");
		}
		
		NoticeInfoDto noticeInfoDto = createNoticeInfoEntity(noticeId, noticeTitle, 
				noticeContext,// 应该改为出去其中的危险信息，比如<script>等脚本注入 
				summaryText, noticePicUrl, noticeType);
		
		noticeService.doAddNoticeInfo(noticeInfoDto);
	
		return "redirect:/notice/showSiteAndComm";
	}
	
	/**
	 * @author HQ
	 * @功能 根据公告id修改公告
	 */
	@RequestMapping("admin/modifyNoticeInfo")
	@ResponseBody
	public Map<String, Object> modifyNoticeInfo(HttpServletRequest request, HttpServletResponse response){
		
		Map<String, Object> noticeMap = new HashMap<String, Object>();
		
		String noticeId = ParamConverter.assertNotEmpty(request.getParameter("noticeId"));
		String noticeTitle = SystemUtil.convertSpecialMark(ParamConverter.assertNotEmpty(request.getParameter("noticeTitle")));
		String noticeContext = SystemUtil.removeAllScriptFromHtml(ParamConverter.assertNotEmpty(request.getParameter("noticeContent")));
		String noticeType = ParamConverter.assertNotEmpty(request.getParameter("noticeType"));
		String noticePicUrl = ParamConverter.assertNotEmpty(request.getParameter("noticeUrl"));
		String summaryText = SystemUtil.removeAllScriptFromHtml(ParamConverter.assertNotEmpty(request.getParameter("noticeSummary")));
		
		NoticeInfoDto noticeInfoDto = createNoticeInfoEntity(noticeId, noticeTitle, 
				noticeContext,// 应该改为出去其中的危险信息，比如<script>等脚本注入 
				summaryText, noticePicUrl, noticeType);
		
		NoticeInfoDto noticeInfo = noticeService.doModifyNotice(noticeInfoDto);
		
		noticeMap.put("result", SystemConstants.AJAX_SUCCESS);
		noticeMap.put("modifyNotice", noticeInfo);
		
		return noticeMap;
	}

	/**
	 * @功能 创建noticeInfoDto类
	 * @param noticeId
	 * @param noticeTitle
	 * @param noticeContext
	 * @param noticePicUrl
	 * @param noticeType
	 * @return
	 */
	private NoticeInfoDto createNoticeInfoEntity(String noticeId, String noticeTitle, String noticeContent, String noticeSummaryText, String noticePicUrl, String noticeType) {
		NoticeInfoDto noticeInfo = new NoticeInfoDto(noticeId);
		
		int nowTime = DateUtil.convertCurrentDTTMtoInt();
		noticeInfo.setNoticeContent(noticeContent);
		noticeInfo.setNoticeSummaryText(noticeSummaryText);
		noticeInfo.setNoticeCreateDttm(nowTime);
		noticeInfo.setNoticePicUrl(noticePicUrl);
		noticeInfo.setNoticeTitle(noticeTitle);
		noticeInfo.setNoticeType(NoticeType.getNoticeType(noticeType));
		
		return noticeInfo;
	}
	
	
	/**
	 * @author HQ
	 * @功能 根据公告id删除公告
	 */
	@RequestMapping("admin/ajaxDeleteNotice")
	@ResponseBody
	public Map<String, Object> deleteNotice(HttpServletRequest request, HttpServletResponse response){
		
		String noticeId = ParamConverter.assertNotEmpty(request.getParameter("noticeId"));
		String decNoticeId = SystemUtil.decrypt(noticeId);
		int realNoticeId = ParamConverter.convertToInteger(decNoticeId);
		noticeService.doDeleteNoticeById(realNoticeId);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		return resultMap;
	}
}
